/**
 * Created by luqidong on 2017/12/12.
 */
import  TDbrideg from '../uitls/bridge/index';
import store from 'store2';
import $router from '../router/index';
import axios from './http';
import {isWeixin} from '../uitls/common';
import {USER_URI} from '../uitls/ajaxUrl';
/**
 * 获取token方法
 * @returns {Promise}
 */
export const getMytoken = (showmsg) => {
    return new Promise((resolve, reject) => {
        //获取token方法 调试可以在这里集体改token
        try {
            TDbrideg.appToken.startAppToken(params => {
                alert(decodeURIComponent(params));
                const tokenogj = JSON.parse(decodeURIComponent(params));
                if(tokenogj.token) {
                    resolve(tokenogj)
                }
                else {
                    if(showmsg) {
                        resolve({})
                    }
                    else {
                        getLogin().then(function (pp) {
                            console.log(6666666);
                        });
                        // Toast.fail('获取token失败', 2);
                        resolve({});
                        // reject('获取token失败')
                    }
                }
            });
        }
        catch (e) {
            let tokenogj = store('userInfo');
            if(tokenogj && tokenogj.token) {
                resolve(tokenogj)
            }
            else {
                if(!showmsg) {
                    if(isWeixin()) {
                        return;
                    }
                    $router.replace('/common/login');
                } else {
                    resolve({})
                }
            }
        }

    });
};
export const getDeviceinfo = () => {
    return new Promise((resolve, reject) => {
        try {
            TDbrideg.appToken.startAppToken(params => {
                const deviceInfo = JSON.parse(decodeURIComponent(params));
                if(deviceInfo.version) {
                    resolve(deviceInfo)
                }
                else {
                    resolve({});
                }
            });
        }
        catch (e) {
            let params = store('token') || '%7B%0A%20%20%22mobile%22%20%3A%20%22153%2A%2A%2A%2A0906%22%2C%0A%20%20%22channel%22%20%3A%20%22IOS%22%2C%0A%20%20%22auditState%22%20%3A%20false%2C%0A%20%20%22havPay%22%20%3A%20true%2C%0A%20%20%22EncryptUserID%22%20%3A%20%2217%22%2C%0A%20%20%22userHead%22%20%3A%20%22http%3A%5C%2F%5C%2Fres.cp.lovedou.com%5C%2Fimg%5C%2FUpload%5C%2F20170727%5C%2F2017072719452981.jpeg%22%2C%0A%20%20%22version%22%20%3A%20%221.0.0%22%2C%0A%20%20%22appCode%22%20%3A%20%222%22%2C%0A%20%20%22token%22%20%3A%20%223d898fdb-271d-4e6b-99c1-5fca1df11edd%22%2C%0A%20%20%22balance%22%20%3A%20%22115.26%22%2C%0A%20%20%22distBalance%22%20%3A%20%22115.26%22%0A%7D';
            const deviceInfo = JSON.parse(decodeURIComponent(params));
            if(deviceInfo.version) {
                resolve(deviceInfo)
            }
            else {
                reject({});
            }
        }

    });
};
export const getAppCode = (showmsg) => {
    return new Promise((resolve, reject) => {
        //获取token方法 调试可以在这里集体改token
        try {
            TDbrideg.appToken.startAppToken(params => {
                const tokenogj = JSON.parse(decodeURIComponent(params));
                if(tokenogj.appCode) {
                    resolve(tokenogj)
                }
                else {
                    resolve({appCode: 1})
                }
            });
        }
        catch (e) {
            //token %7B%0A%20%20%22mobile%22%20%3A%20%22153%2A%2A%2A%2A0906%22%2C%0A%20%20%22channel%22%20%3A%20%22IOS%22%2C%0A%20%20%22auditState%22%20%3A%20false%2C%0A%20%20%22havPay%22%20%3A%20true%2C%0A%20%20%22EncryptUserID%22%20%3A%20%2217%22%2C%0A%20%20%22userHead%22%20%3A%20%22http%3A%5C%2F%5C%2Fres.cp.lovedou.com%5C%2Fimg%5C%2FUpload%5C%2F20170727%5C%2F2017072719452981.jpeg%22%2C%0A%20%20%22version%22%20%3A%20%221.0.0%22%2C%0A%20%20%22appCode%22%20%3A%20%222%22%2C%0A%20%20%22token%22%20%3A%20%2282c92f9c-9ac5-4870-b0d0-8f335c52a4c6%22%2C%0A%20%20%22balance%22%20%3A%20%221.99%22%2C%0A%20%20%22distBalance%22%20%3A%20%220.00%22%0A%7D

            //resolve(decodeURIComponent('%7b%22token%22%3a%22dacf07b9-1bd9-441b-a507-e84e8ddf4b32%22%2c%22passportId%22%3a32%2c%22mobile%22%3a%22153****0906%22%2c%22balance%22%3a%2299998281.00%22%2c%22distBalance%22%3a%2288888888.00%22%7d'));
            let params = store('token') || '%7B%22token%22%3A%227260e56d-beab-44dd-828b-3385ace45100%22%2C%22passportId%22%3A0%2C%22mobile%22%3A%22%22%2C%22balance%22%3A%220.00%22%2C%22distBalance%22%3A%220.00%22%2C%22havPay%22%3Afalse%2C%22EncryptUserID%22%3A%22rC4GsTPoWUY%3D%22%2C%22version%22%3A%221.0.0%22%2C%22channel%22%3A%22td%22%2C%22auditState%22%3Afalse%2C%22appCode%22%3A%221%22%2C%22userHead%22%3A%22http%3A%5C%2F%5C%2Fq.qlogo.cn%5C%2Fqqapp%5C%2F1106180495%5C%2FE30F1C0023C138EA398E8D7E1400DFB6%5C%2F100%22%7D';

            const tokenogj = JSON.parse(decodeURIComponent(params));
            if(tokenogj.token || tokenogj.version) {
                resolve(tokenogj)
            }
            else {
                Toast.fail('获取token失败', 2);
                reject('获取token失败');
            }
        }

    });
};

export const getLogin = (fn) => {
    return new Promise((resolve, reject) => {
        try {
            TDbrideg.loginPlugin.login(params => {
                fn && fn(params);
            });
        }
        catch (e) {
            console.log('inlogin2', e);
        }

    });
};

export const getLogintoken = (fn)=> {
    return new Promise((resolve, reject) => {
        //获取token方法 调试可以在这里集体改token
        try {
            TDbrideg.appToken.startAppToken(params => {
                console.warn('token', params);
                const tokenogj = JSON.parse(decodeURIComponent(params));
                if(tokenogj.token) {
                    resolve(tokenogj)
                }
                else {
                    getLogin(fn).then(function (pp) {
                        console.warn('pp', pp);
                        resolve({reload: true});
                    });
                    // Toast.fail('获取token失败', 2);
                    resolve({});
                    // reject('获取token失败')
                }
            });
        }
        catch (e) {
            //token %7B%22token%22%3A%22cf3fc335-5ecf-45b4-ae03-a917dc432c0f%22%2C%22passportId%22%3A0%2C%22mobile%22%3A%22153****0906%22%2C%22balance%22%3A%220.00%22%2C%22distBalance%22%3A%220.00%22%2C%22havPay%22%3Atrue%2C%22EncryptUserID%22%3A%2215%22%2C%22version%22%3A%221.0.0%22%2C%22channel%22%3A%22mi%22%2C%22auditState%22%3Afalse%2C%22appCode%22%3A%2255%22%2C%22userHead%22%3A%22http%3A%5C%2F%5C%2Fres.cp.lovedou.com%5C%2Fimg%5C%2FUpload%5C%2F20170828%5C%2F2017082809213761.jpeg%22%7D
            //resolve(decodeURIComponent('%7b%22token%22%3a%22dacf08b9-1bd9-441b-a507-e84e8ddf4b32%22%2c%22passportId%22%3a32%2c%22mobile%22%3a%22153****0906%22%2c%22balance%22%3a%2299998281.00%22%2c%22distBalance%22%3a%2288888888.00%22%7d'));
            let params = store('token') || '%7B%22token%22%3A%22cf3fc335-5ecf-45b4-ae03-a917dc432c0f%22%2C%22passportId%22%3A0%2C%22mobile%22%3A%22153****0906%22%2C%22balance%22%3A%220.00%22%2C%22distBalance%22%3A%220.00%22%2C%22havPay%22%3Atrue%2C%22EncryptUserID%22%3A%2215%22%2C%22version%22%3A%221.0.0%22%2C%22channel%22%3A%22mi%22%2C%22auditState%22%3Afalse%2C%22appCode%22%3A%2255%22%2C%22userHead%22%3A%22http%3A%5C%2F%5C%2Fres.cp.lovedou.com%5C%2Fimg%5C%2FUpload%5C%2F20170828%5C%2F2017082809213761.jpeg%22%7D';

            const tokenogj = JSON.parse(decodeURIComponent(params));
            if(tokenogj.token || tokenogj.version) {
                resolve(tokenogj)
            }
            else {
                Toast.fail('获取token失败', 2);
                reject('获取token失败');
            }
        }

    });
};
/**
 *
 * @param BackUrl
 * @returns {*}
 */
export function authorizationR(BackUrl) {
    return axios.get(`${USER_URI}/Handler.ashx?action=1611&params={"BackUrl":"${BackUrl}"}`);
}
/**
 * 授权参数获取
 * @param code
 * @returns {*}
 */
export function authorizationcodeR(code) {
    return axios.get(`${USER_URI}/Handler.ashx?action=1612&params={"code":"${code}","RegionID":"-1"}`);
}